<?php
namespace com\mvc\model;

use com\mvc\core\Request;
class LoginModel extends \com\mvc\model\ModelData{
	public function __construct(){
		//parent::__construct($name, $firstname, $age, $address, $telephone);
	}
	public function model($path){
		$login = new \com\mvc\domain\LoginData();
		$request = new \com\mvc\core\Request();
		if (strtoupper($request->method())=='GET'){
			$request->bindingObject($login, $_GET);
		}else if (strtoupper($request->method())=='POST'){
			$request->bindingObject($login, $_POST);
		}
		
		$db = \com\mvc\orm\DB::getInstance();
		
		//$db->forcedQuery("SELECT count(1) as valor FROM usuarios WHERE nombre = :user AND clave = :password");
		$db->forcedQuery("SELECT nombre,clave FROM usuarios WHERE nombre = ? AND clave = ?");
		$params = array(new \com\mvc\orm\dao\GenericObjectDB($login->getUser(),':user','string')
						,new \com\mvc\orm\dao\GenericObjectDB($login->getPassword(),':password','string'));
		$db->addBindingParams($params);
		$db->addColumn("nombre","clave");
		$result = $db->query();
		$db->closeConnection();
		
		// Para simular la BBDD
		//$result = array(array('valor'=>1));
		
		$var = array('user'=>$login->getUser(),'password'=>$login->getPassword());
		if (isset($result) && count($result)==1){
			$sesion = new \com\mvc\core\SessionUser();
			$sesion->loadDataSession(array('user'=>$login->getUser()));
			$res = $sesion->validateSession();
			$this->render(PATH_VIEWS.ucfirst($path).VIEW.EXTENSION,$var);
		}else{
			$this->render(PATH_VIEWS.INDEX.VIEW.EXTENSION,$var);
		}
		
		
	}
}
?>